﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Threading;
using System.Data;

namespace Kkc.Model
{
    public partial class KkcDataEntities
    {
        partial void OnContextCreated()
        {
            SavingChanges += OnSavingChanges;
        }

        private void OnSavingChanges(object sender, EventArgs e)
        {
            foreach (var objectStateEntry in ObjectStateManager.GetObjectStateEntries(EntityState.Added | EntityState.Modified))
            {
                var entity = objectStateEntry.Entity as IChangeTracking;
                if (entity == null)
                    continue;

                entity.ModifiedByUserId = Thread.CurrentPrincipal.Identity.Name;
                entity.ModificationDate = DateTime.Now;

                if (objectStateEntry.State == EntityState.Added)
                {
                    entity.CreatedByUserId = entity.ModifiedByUserId;
                    entity.CreationDate = entity.ModificationDate;
                }
            }
        }
    }
}